package com.horoscope.astrology.zodiac.signs.free.activity

import android.os.Bundle
import android.util.Log
import androidx.viewpager.widget.ViewPager
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.adapter.MainPageFragmentAdapter
import com.horoscope.astrology.zodiac.signs.free.bean.CityInfo
import com.horoscope.astrology.zodiac.signs.free.bean.UserBean
import com.horoscope.astrology.zodiac.signs.free.fragment.BirthdayFragment
import com.horoscope.astrology.zodiac.signs.free.fragment.LoadFragment
import com.horoscope.astrology.zodiac.signs.free.view.NoScrollViewPager
import com.horoscope.astrology.zodiac.signs.free.view.Xcircleindicator

/**
 *  注册页由6个fragment组成（禁止滑动）
 * */
class PreMainActivity : BaseActivity() {

//    private lateinit var mNameFragment: NameFragment
//    private lateinit var mGenderFragment: GenderFragment
//    private lateinit var mBornTimeFragment: BornTimeFragment
//    private lateinit var mRelationshipFragment: RelationshipFragment

    private lateinit var mBirthdayFragment: BirthdayFragment
    private lateinit var mLoadFragment: LoadFragment
    private lateinit var mAdapter: MainPageFragmentAdapter
    private lateinit var mViewPager: NoScrollViewPager //https://blog.csdn.net/yilei0033/article/details/79444099
    private val userBean = UserBean()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_pre_main)
        userBean.name = "Your name"
        //AndroidBug5497Workaround.assistActivity(this)
        initView()
        setAdapter()

    }


    fun getUserBean(): UserBean {
        return userBean
    }

    fun setUserName(name: String) {
        userBean.name = name

    }

    fun setCityInfo(bean: CityInfo) {
        userBean.cityInfo = bean
    }

    fun setBornTime(time: String) {
        userBean.bornTime = time
        Log.d("ASDADAFADFS", time)
    }


    fun setBirthday(birthday: String) {
        userBean.birthday = birthday
    }

    fun setBornPlace(place: String) {
        userBean.bornPlace = place
    }

    fun setMarial(status: String) {
        userBean.marial_status = status
    }

    fun setGender(gender: String) {
        userBean.gender = gender
    }

    fun setConstellationName(name: String) {
        userBean.constellationName = name
    }

    private fun initView() {
//        mNameFragment = NameFragment()
//        mGenderFragment = GenderFragment()
//        mBornTimeFragment = BornTimeFragment()
//        mRelationshipFragment = RelationshipFragment()
        mBirthdayFragment = BirthdayFragment()
        mLoadFragment = LoadFragment()
        mViewPager = findViewById(R.id.view_pager)
        mViewPager.setNoScroll(true)
    }

    private var mXcircleindicator: Xcircleindicator? = null

    private fun setAdapter() {
        mXcircleindicator = findViewById(R.id.Xcircleindicator)

        mAdapter = MainPageFragmentAdapter(this, supportFragmentManager)
//        mAdapter.setNameFragment(mNameFragment)
//        mAdapter.setGenderFragment(mGenderFragment)
//        mAdapter.setBornFragment(mBornTimeFragment)
//        mAdapter.setRelationshipFragment(mRelationshipFragment)
        mAdapter.setBirthdayFragment(mBirthdayFragment)
        mAdapter.setLoadFragment(mLoadFragment)
        mViewPager.adapter = mAdapter
        mXcircleindicator?.initData(mAdapter.count, 0)
        mXcircleindicator?.setCurrentPage(0)
        mViewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(p0: Int) {
            }

            override fun onPageScrolled(p0: Int, p1: Float, p2: Int) {
            }

            override fun onPageSelected(p0: Int) {
                mXcircleindicator?.setCurrentPage(p0)
                when (p0) {
                    1 -> {
                        if (mLoadFragment.isAdded) {
                            mLoadFragment.fadeInRoot()
                            mLoadFragment.startAnim()
                            mLoadFragment.setName(userBean.name)
                        }
                    }
/*                    1 -> {
                        if (mGenderFragment.isAdded) {
                            mGenderFragment.fadeIn()
                            mGenderFragment.setName(userBean.name)
                        }

                    }
                    2 -> {
                        if (mBirthdayFragment.isAdded) {
                            mBirthdayFragment.fadeIn()
                            mBirthdayFragment.setName(userBean.name)
                        }
                    }
                    3 -> {
                        if (mBornTimeFragment.isAdded) {
                            mBornTimeFragment.fadeIn()
                            mBornTimeFragment.setConstellationName(userBean.constellationName)
                            mBornTimeFragment.setName(userBean.name)
                        }

                    }
                    4 -> {
                        if (mRelationshipFragment.isAdded) {
                            mRelationshipFragment.fadeIn()
                            mRelationshipFragment.setConstellationName(userBean.constellationName)
                            mRelationshipFragment.setName(userBean.name)
                        }
                    }
                    5 -> {
                        if (mLoadFragment != null && mLoadFragment!!.isAdded) {
                            mLoadFragment!!.fadeInRoot()
                            mLoadFragment!!.startAnim()
                            mLoadFragment!!.setName(userBean.name)
                        }
                }*/
                }

            }

        })
    }

    fun setPage(count: Int) {
        if (count >= 0 && count < mAdapter.count) {
            mViewPager.currentItem = count
        }
    }
}
